#!/bin/bash

. functions.sh

# Install Apache and PHP (and any needed extensions).
# Moved to Puppet script, see Makefile, ../Vagrantfile and ../manifests/site.pp

# We have added the domain to DNS
# Add hosts file entry
#if ! grep -q "web-forms.local" /etc/hosts; then
#    sudo sh -c ' echo "127.0.0.1 web-forms.local" >> /etc/hosts'
#fi

# Moved to Puppet, see modules/smx_os/files/php.ini
# Make sure the timezone is set in php.ini.
#echo "Updating timezone in php.ini..."
#sudo sed -i".bak" "s/^\;date\.timezone.*$/date\.timezone = \"America\\/Los_Angeles\" /g" /etc/php.ini
#sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /etc/php.ini

# Set ServerName for apache
if [ ! -e "/etc/httpd/conf.d/servername.conf" ]; then
    echo "Setting servername in for apache"
    sudo sh -c 'echo "ServerName web-forms.local" >> /etc/httpd/conf.d/servername.conf'
fi

# Turn off Apache's EnableSendfile option (for information on why, see
# "http://www.geoffmcqueen.com/2011/08/12/solved-apache-serving-corrupted-files-via-virtualbox-vboxfs-shares/").
sudo sed -i "s/#EnableSendfile off/EnableSendfile off/g" /etc/httpd/conf/httpd.conf
# Let's run Apache as vagrant/vagrant to avoid user permission hassles
sudo sed -i "s/User apache/User vagrant/g" /etc/httpd/conf/httpd.conf
sudo sed -i "s/Group apache/Group vagrant/g" /etc/httpd/conf/httpd.conf


# Make sure Apache is configured to start automatically and is running.
echo "Ensuring httpd is running and enabled at startup..."
sudo chkconfig httpd on

#Make apache access to session file configure in php.ini
sudo chown -Rf vagrant.vagrant /var/lib/php/session
sudo chown -Rf vagrant.vagrant /tmp/fp
sudo chown -Rf vagrant.vagrant /tmp/forms-publisher

# Copy the vhost conf file to where Apache will find it.
sudo \cp $SOURCE_ROOT/vagrant/vhost-local.conf /etc/httpd/conf.d/
# Copy the api.json file to where platform will find it
sudo \cp $SOURCE_ROOT/vagrant/api.json /etc/foxitcloud/

if [ -n "$PC_ENV" ]; then
	sudo sed -i "s/vagrant./$PC_ENV./g" /etc/httpd/conf.d/vhost-local.conf	
#	sudo sed -i "s/www.*foxitcloud.com/www.${HOST_ENV}/g" /etc/httpd/conf.d/vhost-local.conf
	
	sudo sed -i "s/vagrant./$PC_ENV./g" /etc/foxitcloud/api.json
	#sudo sed -i "s/-vagrant./-$PC_ENV./g" $SOURCE_ROOT/webapp/connectedpdfapp/web/dll-config/1.0/cpdfApi.json
	sudo sed -i "s/vagrant./$PC_ENV./g" $SOURCE_ROOT/webapp/connectedpdfapp/web/dll-config/1.0/cpdfApi.json
fi

if [[ "$YUM_SYS" =~ ^7\..* ]]; then
        sudo sed -i -e"/Order allow,deny/,+1d" -e"/AllowOverride FileInfo/a\    Require all granted" /etc/httpd/conf.d/vhost-local.conf
fi

#Copy Apache ssl config
if [[ "$YUM_SYS" =~ ^6\..* ]]; then
        sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/ssl.conf /etc/httpd/conf.d/
elif [[ "$YUM_SYS" =~ ^7\..* ]]; then
        sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/ssl.conf /etc/httpd/conf.d/
        sudo sed -i "s/SSLMutex/Mutex/" /etc/httpd/conf.d/ssl.conf
fi
sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/foxitcloud.crt /etc/httpd/conf.d/
sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/foxitcloud.key /etc/httpd/conf.d/
sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/connectedpdf-bundle.crt /etc/httpd/conf.d/
sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/connectedpdf-crt.crt /etc/httpd/conf.d/
sudo cp $SOURCE_ROOT/vagrant/svcconfig/httpd_config/connectedpdf.com.key /etc/httpd/conf.d/
# Adjust the mode settings on that conf file.
sudo chmod 644 /etc/httpd/conf.d/vhost-local.conf

# Restart Apache.
echo "Restarting apache"
sudo service httpd restart

